function mocarousel(){
  var car = {};
  car.current = 0;
  car.obj = {};
  car.info = {};
  car.info.dim = {};
  car.info.amt = '';
  car.info.li = {};
  
  //Initialises the carousel
  car.init = function() {
    car.obj.main = $('#mocarousel');
    car.obj.li = car.obj.main.find('li');
    car.info.li.w = parseInt(car.obj.li.eq(0).css('width'));
    car.info.li.h = parseInt(car.obj.li.eq(0).css('height'));
    car.info.amt = car.obj.li.length;
    car.info.dim.h = parseInt(car.obj.main.css('height'));
    car.info.dim.w = parseInt(car.obj.main.css('width'));
    car.info.incs = Math.floor(car.info.dim.w/8);
    
    var startpt = Math.floor(car.info.amt/2);
    car.current = parseInt(car.obj.li.eq(startpt).attr('rel'));
    car.place(startpt);
    
    car.obj.main.find('.prev').click(function(){
      car.prev();
    });
    car.obj.main.find('.next').click(function(){
      car.next();
    });
    car.obj.li.bind('mouseenter', function(){if ($(this).attr('rel') == car.current) {car.showOverlay();}});
    car.obj.main.bind('mouseleave', car.hideOverlay);
    car.obj.li.click(function() {
      if ($(this).attr('rel') == car.current) {
        car.play();
      }
      else {
        car.moveTo(parseInt($(this).attr('rel')));
      }
    });
  }
  
  //Places the elements upon initialisation
  car.place = function(pos) {
    cobj = car.obj.li;//cobj.find('img').css({'height' : (car.info.li.h*(1-(2/5)))+'px','width' : (car.info.li.w*(1-(2/10)))+'px'});
    cobj.find('img').css({'height' : (car.info.li.h*(1-(2/5)))+'px','width' : '100%'});
    cobj.hide().css('top', (Math.floor(car.info.dim.h/2) - Math.floor(car.info.li.h/2) + Math.floor(car.info.li.h*(2/10)))+'px');
    
    /*Places non-visible images so they can come out nicely when called*/
    for (var i = pos+2; i <= car.info.amt; i++) {cobj.eq(i).css('left', ((car.info.incs*(5)) - Math.floor(car.info.li.w/2))+'px');}
    for (var i = 0; i <= (pos-2); i++) {cobj.eq(i).css('left', ((car.info.incs*(1)) - Math.floor(car.info.li.w/2))+'px');}
    
    //Placing visible images
    for (i= -2; i <= 2; i++) {
      if (i < 0) { z = (-1*i);}
      else {z= i;}
      y = i+2;
      cobj.eq(pos+i).show()
      .css({
        'left' : (car.info.incs*(y))+'px',
        'top': (Math.floor(car.info.dim.h/2) - Math.floor(car.info.li.h/2) + Math.floor(car.info.li.h*(z/10)))+'px',
        'z-index': 2-z
      });
      cobj.eq(pos+i).find('img').css({
        'height' : (car.info.li.h*(1-(z/5)))+'px',//'width' : (car.info.li.w*(1-(z/10)))+'px'
        'width' : '100%'
      });
    }
  }
  
  //Moves the elements per click
  car.move = function(pos) {
    cobj = car.obj.li;
    cobj.hide();
    cobj.find('.overlay, iframe, .play').hide();
    for (var i= -2; i <= 2; i++) {
      if (i < 0) { z = (-1*i);}
      else {z= i;}
      y = i+2;
      if (pos+i >= 0) {
        cobj.eq(pos+i).show().stop(true,false)
        .animate({
          'left' : (car.info.incs*(y))+'px',
          'top': (Math.floor(car.info.dim.h/2) - Math.floor(car.info.li.h/2) + Math.floor(car.info.li.h*(z/10)))+'px'
        })
        .css('z-index', 2-z);
        cobj.eq(pos+i).find('img').stop(true,false).animate({
          'height' : (car.info.li.h*(1-(z/5)))+'px',//'width' : (car.info.li.w*(1-(z/10)))+'px'
          'width' : '100%'
        });
      }
    }
  }
  
  //Moves elements directly to a location
  car.moveTo = function(finaldest){
    if (car.current < finaldest) {while (car.current != finaldest) {car.current +=1; car.move(car.current)};}
    else if (car.current > finaldest) {while (car.current != finaldest) {car.current -= 1; car.move(car.current)};}
  }
  
  //Starts player
  car.play = function() {car.obj.li.eq(car.current).find('iframe').show();}
  car.hideOverlay = function() {
    car.obj.li.eq(car.current).find('.overlay').stop(true, true).slideUp();
    car.obj.li.eq(car.current).find('.play').stop(true, true).fadeOut();
  }
  car.showOverlay = function() {
    car.obj.li.eq(car.current).find('.overlay').stop(true, true).slideDown();
    car.obj.li.eq(car.current).find('.play').stop(true, true).fadeIn();
  }
  car.next = function() {
    if (car.current != (car.info.amt-1)) {
      car.current += 1;
      car.move(car.current);
    }
  }
  car.prev = function() {
    if (car.current != 0) {
      car.current -= 1;
      car.move(car.current);
    }
  }
  return car;
}
var gallery = new mocarousel();